import { seekElement_class, seekElement_id } from '../Func/seek_element';

const event_checkTag = 'event_check';

const specialKeyEnumevent = {
  altKey: 'altKey',
  ctrlKey: 'ctrlKey',
  metaKey: 'metaKey',
  shiftKey: 'shiftKey',
} as const;

export class event_check {
  get [Symbol.toStringTag]() {
    return event_checkTag;
  }

  constructor() {}

  result: boolean = true;

  /**
   * 设置 最终检测结果
   * @param {Boolean} bool
   */
  #set_checked_result(bool: boolean) {
    this.result = this.result && bool;
  }

  event?: Event;

  /**
   * 特别 class 名 元素
   */
  special_class_dom?: HTMLElement;

  /**
   * 重新 设置 当前
   */
  set_cur_event(event: Event) {
    this.event = event;
    this.result = true;
    return this;
  }

  /**
   * 检测 event 出发元素 是否 为 绑定 事件 的元素
   * @returns
   */
  is_itself() {
    if (this.event == undefined) {
      console.log(`make sure first set current event please`);
      this.#set_checked_result(false);
      return this;
    }

    this.#set_checked_result(this.event.target == this.event.currentTarget);
    return this;
  }

  /**
   * 是否 左键 点击
   * @returns
   */
  left_click() {
    this.#set_checked_result((this.event as MouseEvent).button == 0);
    return this;
  }

  /**
   * 是否 右键 点击
   * @returns
   */
  right_click() {
    this.#set_checked_result((this.event as MouseEvent).button == 2);
    return this;
  }

  /**
   * 检测 当前 已经按下的 keyBoard
   */
  specialKey_before_check(beforeKeyB: string[] = []) {
    let ret = true;
    let key: keyof KeyboardEvent;

    for (key in specialKeyEnumevent) {
      if ((this.event as KeyboardEvent)[key]) {
        if (beforeKeyB.indexOf(key) > -1) {
          continue;
        }
        ret = false;
      }
    }

    this.#set_checked_result(ret);

    return this;
  }

  /**
   * 检测 当前 按下的 keyBoard
   */
  cur_specialkey_check(curKey: keyof KeyboardEvent) {
    let cur_event: KeyboardEvent = this.event as KeyboardEvent;
    this.#set_checked_result([cur_event.key, cur_event.code].includes(curKey));

    return this;
  }

  /**
   * 找寻 特定 id
   * @param targetEl
   * @returns
   */
  is_child_id_emit(targetEl: HTMLElement) {
    if (this.event == undefined) {
      console.log(`make sure first set current event please`);
      this.#set_checked_result(false);
      return this;
    }

    let parentEl = seekElement_id(
      this.event.target as HTMLElement,
      targetEl.id
    );

    this.#set_checked_result(parentEl == targetEl);
    return this;
  }

  /**
   * 找寻 特定 class 祖先
   * @param cla
   * @returns
   */
  is_chi_class_emit(cla: string) {
    if (this.event == undefined) {
      console.log(`make sure first set current event please`);
      this.#set_checked_result(false);
      return this;
    }

    let parentEl = seekElement_class(this.event.target as HTMLElement, cla);

    parentEl !== null
      ? (this.#set_checked_result(true), (this.special_class_dom = parentEl!))
      : this.#set_checked_result(false);

    return this;
  }

  // #check_cur_event_not_undefined() {
  //   if (this.event == undefined) {
  //     console.log(`make sure first set current event please`);
  //     this.#set_checked_result(false);
  //   }
  //   return this.event == undefined;
  // }
}

export const cur_event_check = new event_check();
